package com.mytijian.task.controller;

import com.mytijian.task.enums.TaskStatusEnum;
import com.mytijian.task.repository.redis.RuningTaskRedisRepository;
import com.mytijian.task.task.TaskIdentifyOperateService;
import com.mytijian.task.executor.GroupExecutorService;
import com.mytijian.task.repository.model.GroupExecutorDTO;
import com.mytijian.task.util.IpAddressUtil;
import com.mytijian.task.model.TaskIdentify;
import com.mytijian.task.model.TaskValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/workNode")
public class WorkNodeTaskController {
    @Autowired
    private TaskIdentifyOperateService taskIdentifyOperateService;
    @Autowired
    private RuningTaskRedisRepository runingTaskRedisRepository;
    private String OK = "OK";
    /**
     * 终止
     * @param taskId
     */
    @GetMapping("/stop")
    @ResponseBody
    public String stop( Integer taskId) {
        taskIdentifyOperateService.stop(taskId);
        return OK;
    }

    /**
     * 暂停
     * @param taskId
     */
    @GetMapping("/suspend")
    @ResponseBody
    public String suspend(Integer taskId) {
        taskIdentifyOperateService.suspend(taskId);
        return OK;
    }

    /**
     * 处理
     * @param taskId
     */
    @GetMapping("/process")
    @ResponseBody
    public String process(Integer taskId) {
        taskIdentifyOperateService.process(taskId);
        return OK;
    }
    @GetMapping("/processAll")
    @ResponseBody
    public String beginAll()  {
        Map<TaskIdentify,TaskValue> taskValueMap = runingTaskRedisRepository.getByIp(IpAddressUtil.getLocalHost());
        if (CollectionUtils.isEmpty(taskValueMap)) {
            return OK;
        }
        taskValueMap.forEach(((taskIdentify, taskValue) -> {
            if (!TaskStatusEnum.getEndStatusInteger().contains(taskValue.getTaskStuts())) {
                taskIdentifyOperateService.process(taskIdentify.getTaskId());
            }
        }));
        return OK;
    }
    @PostMapping(value = "/addGroupExecutor")
    public String addGroupExecutor(@RequestBody GroupExecutorDTO groupExecutorDTO) {
        GroupExecutorService.GroupExecutorParam param = new GroupExecutorService.GroupExecutorParam(groupExecutorDTO.getWorkThreads(), groupExecutorDTO.getCapacity(), groupExecutorDTO.getGroupKey());
        GroupExecutorService.addGroupExecutor(param);
        return OK;
    }
    @PostMapping(value = "modifyGroupExecutor")
    public String modifyGroupExecutor(@RequestBody GroupExecutorDTO groupExecutorDTO) {
        GroupExecutorService.GroupExecutorParam param = new GroupExecutorService.GroupExecutorParam(groupExecutorDTO.getWorkThreads(), groupExecutorDTO.getCapacity(), groupExecutorDTO.getGroupKey());
        GroupExecutorService.modifyExecutor(param);
        return OK;
    }
    @GetMapping("/isOK")
    public String test() {
        return OK;
    }
}
